# Run QAP -- followers 
rm(list = ls())

library(sna)
library(doParallel)
library(doRNG)


# Followers adjacency matrix
y <- readRDS("processed_data/followers_adjacencyMatrix.Rds")

# load similarity matrix(s)
state_similarity_matrix <- readRDS("processed_data/state_similarity_matrix.Rds")
party_similarity_matrix <- readRDS("processed_data/party_similarity_matrix.Rds")
chamber_similarity_matrix <- readRDS("processed_data/chamber_similarity_matrix.Rds")
gender_similarity_matrix <- readRDS("processed_data/gender_similarity_matrix.Rds")
race_similarity_matrix <- readRDS("processed_data/same_race.Rds")
profesh_diff_matrix <- readRDS("processed_data/profeshScore_diff.Rds")

# Sender matrix
democrat_xs <- readRDS("processed_data/democrat_sender_effect.Rds")
republican_xs <- readRDS("processed_data/republican_sender_effect.Rds")
house_xs <- readRDS("processed_data/house_sender_matrix.Rds")
gender_xs <- readRDS("processed_data/gender_sender_matrix.Rds")
profesh_xs <- readRDS("processed_data/sender_profesh.Rds")
black_xs <- readRDS("processed_data/sender_black.Rds")
latino_xs <- readRDS("processed_data/sender_latino.Rds")
asian_xs <- readRDS("processed_data/sender_asian.Rds")
mena_xs <- readRDS("processed_data/sender_mena.Rds")
multi_xs <- readRDS("processed_data/sender_multi.Rds")
native_xs <- readRDS("processed_data/sender_native.Rds")

# Receiver effects
democrat_xr <- readRDS("processed_data/democrat_receiver_effect.Rds")
republican_xr <- readRDS("processed_data/republican_receiver_effect.Rds")
house_xr <- readRDS("processed_data/house_receiver_matrix.Rds")
gender_xr <- readRDS("processed_data/gender_receiver_matrix.Rds")
profesh_xr <- readRDS("processed_data/receiver_profesh.Rds")
black_xr <- readRDS("processed_data/receiver_black.Rds")
latino_xr <- readRDS("processed_data/receiver_latino.Rds")
asian_xr <- readRDS("processed_data/receiver_asian.Rds")
mena_xr <- readRDS("processed_data/receiver_mena.Rds")
multi_xr <- readRDS("processed_data/receiver_multi.Rds")
native_xr <- readRDS("processed_data/receiver_native.Rds")

# Interaction effects
party_sameState_interaction_matrix <- readRDS("processed_data/party_sameState_interaction_matrix.Rds")
chamber_sameState_interaction_matrix <- readRDS("processed_data/chamber_sameState_interaction_matrix.Rds")
gender_sameState_interaction_matrix <- readRDS("processed_data/gender_sameState_interaction_matrix.Rds")
race_sameState_interaction_matrix <- readRDS("processed_data/race_sameState_interaction_matrix.Rds")


# Contiguos  states
contiguous <- readRDS("processed_data/contig_states_matrix.Rds")

# join the predicting matrices together
predicting_matrices <- array (NA, c(33, 
                                    length(state_similarity_matrix[1,]), 
                                    length(state_similarity_matrix[1,])))

# Similarity
predicting_matrices[1,,] <- state_similarity_matrix
rm(state_similarity_matrix)

predicting_matrices[2,,] <- party_similarity_matrix
rm(party_similarity_matrix)

predicting_matrices[3,,] <- chamber_similarity_matrix
rm(chamber_similarity_matrix)

predicting_matrices[4,,] <- gender_similarity_matrix
rm(gender_similarity_matrix)

predicting_matrices[5,,] <- race_similarity_matrix
rm(race_similarity_matrix)

predicting_matrices[6,,] <- profesh_diff_matrix
rm(profesh_diff_matrix)


# Sender Effects
predicting_matrices[7,,] <- democrat_xs
rm(democrat_xs)

predicting_matrices[8,,] <- republican_xs
rm(republican_xs)

predicting_matrices[9,,] <- house_xs
rm(house_xs)

predicting_matrices[10,,] <- gender_xs
rm(gender_xs)

predicting_matrices[11,,] <- profesh_xs
rm(profesh_xs)

predicting_matrices[12,,] <- black_xs
rm(black_xs)

predicting_matrices[13,,] <- latino_xs
rm(latino_xs)

predicting_matrices[14,,] <- asian_xs
rm(asian_xs)

predicting_matrices[15,,] <- mena_xs
rm(mena_xs)

predicting_matrices[16,,] <- multi_xs
rm(multi_xs)

predicting_matrices[17,,] <- native_xs
rm(native_xs)


# Receiver Effects
predicting_matrices[18,,] <- democrat_xr
rm(democrat_xr)

predicting_matrices[19,,] <- republican_xr
rm(republican_xr)

predicting_matrices[20,,] <- house_xr
rm(house_xr)

predicting_matrices[21,,] <- gender_xr
rm(gender_xr)

predicting_matrices[22,,] <- profesh_xr
rm(profesh_xr)

predicting_matrices[23,,] <- black_xr
rm(black_xr)

predicting_matrices[24,,] <- latino_xr
rm(latino_xr)

predicting_matrices[25,,] <- asian_xr
rm(asian_xr)

predicting_matrices[26,,] <- mena_xr
rm(mena_xr)

predicting_matrices[27,,] <- multi_xr
rm(multi_xr)

predicting_matrices[28,,] <- native_xr
rm(native_xr)

# Interaction Effects
predicting_matrices[29,,] <- party_sameState_interaction_matrix
rm(party_sameState_interaction_matrix)

predicting_matrices[30,,] <- chamber_sameState_interaction_matrix
rm(chamber_sameState_interaction_matrix)

predicting_matrices[31,,] <- gender_sameState_interaction_matrix
rm(gender_sameState_interaction_matrix)

predicting_matrices[32,,] <- race_sameState_interaction_matrix
rm(race_sameState_interaction_matrix)

# Contiguous States 
predicting_matrices[33,,] <- contiguous
rm(contiguous)

# run QAP in Parallel
#cl <- makeCluster(2, outfile="")  #11
#registerDoParallel(cl)

#set.seed(10)
#system.time(qap_res <- foreach(i=1:2,.packages=c("sna", "doRNG")) %dorng% {
#  netlm(y, predicting_matrices, reps=2)
#})

#stopCluster(cl)

start.time <- Sys.time()
# Set seed value 
seed_value <- 8
set.seed(seed_value)
# Run netlm
qap_res <- netlogit(y, predicting_matrices, reps = 10)
end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken

# Save results 
file_name <- paste0("output/qap_res_followers_out", seed_value, ".Rds")
saveRDS(qap_res, file=file_name)
